package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page, queryVo);
    }

    @Override
    public void customGetPage(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.customSelectById(page, id);
    }

    /**
 * 根据房间ID自定义获取房间详细信息
 * 此方法首先从数据库中获取房间信息，然后获取与该房间相关的所有详细信息，
 * 包括公寓信息、图片、属性、设施、标签、支付方式、租赁条款和费用信息
 * 最后，将所有这些信息封装到一个RoomDetailVo对象中并返回
 *
 * @param id 房间的唯一标识符
 * @return 返回包含房间详细信息的RoomDetailVo对象，如果房间不存在则返回null
 */
@Override
public RoomDetailVo customGetById(Long id) {
    // 根据ID获取房间信息
    RoomInfo roomInfo = roomInfoMapper.selectById(id);
    if (roomInfo == null) {
        return null;
    }

    // 获取公寓信息
    ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
    // 获取房间图片列表
    List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
    // 获取房间属性列表
    List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
    // 获取房间设施列表
    List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
    // 获取房间标签列表
    List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
    // 获取房间支付方式列表
    List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
    // 获取房间租赁条款列表
    List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
    // 获取公寓费用信息列表
    List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());

    // 准备查询租赁协议的状态
    LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(LeaseAgreement::getRoomId, id);
    queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
    // 执行查询并获取符合条件的租赁协议数量
    long count = leaseAgreementMapper.selectCount(queryWrapper);

    // 创建RoomDetailVo对象并设置属性
    RoomDetailVo appRoomDetailVo = new RoomDetailVo();
    BeanUtils.copyProperties(roomInfo, appRoomDetailVo);
    appRoomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
    appRoomDetailVo.setIsCheckIn(count > 0);

    // 设置房间详细信息
    appRoomDetailVo.setApartmentItemVo(apartmentItemVo);
    appRoomDetailVo.setGraphVoList(graphVoList);
    appRoomDetailVo.setAttrValueVoList(attrValueVoList);
    appRoomDetailVo.setFacilityInfoList(facilityInfoList);
    appRoomDetailVo.setLabelInfoList(labelInfoList);
    appRoomDetailVo.setPaymentTypeList(paymentTypeList);
    appRoomDetailVo.setLeaseTermList(leaseTermList);
    appRoomDetailVo.setFeeValueVoList(feeValueVoList);

    // 返回封装好的RoomDetailVo对象
    return appRoomDetailVo;
}

}




